<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2015 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Comment.php 16298 2019-01-28 14:12:48 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_comment 评论
 * +----------------------------------------------------------------------
 */

namespace application\comment\controller;

use application\admin\controller\base as auth;
use ticky\request;

class comment extends auth {

    // 首页
    public function index() {
        global $_G;
        //debug($_G);

        $search = $this->search_frm();
        $ret = db('comment')->where($search)->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->display('comment/index');
    }

    // 添加记录
    public function add() {
        if (request::isPost()) {
            $data = $this->post_frm();
            db('comment')->insert($data);
            showmsg('添加成功', '/comment/comment');
        } else {
            $this->assign('postUrl', '/comment/comment/add');
            $this->assign('action', L('add'));
            $this->display('comment/manage');
        }
    }

    // 批量删除记录
    public function delete() {
        if ($_POST && is_array($_POST['ids'])) {
            $comment = db('comment');
            foreach ($_POST['ids'] as $val) {
                $comment->delete(array('cid' => $val));
            }
        } else {
            db('comment')->delete(['cid' => request::get('id', '0')]);
        }
        showmsg(L('operation_success'), '/comment/comment/index');
    }

    //查询条件
    private function search_frm() {
        $where = '1=1 ';
        $type = request::get('type', '');
        $keyword = request::get('keyword', '');
        if ($keyword != '') {
            switch (strtolower($type)) {
                case "cid":
                    $where .= "and cid = '" . $keyword . "' ";
                    break;
                case "upid":
                    $where .= "and upid = '" . $keyword . "' ";
                    break;
                case "uid":
                    $where .= "and uid = '" . $keyword . "' ";
                    break;
                case "idtype":
                    $where .= "and idtype = '" . $keyword . "' ";
                    break;
                case "id":
                    $where .= "and id = '" . $keyword . "' ";
                    break;
                case "message":
                    $where .= "and message = '" . $keyword . "' ";
                    break;
                case "ip":
                    $where .= "and ip = '" . $keyword . "' ";
                    break;
                case "status":
                    $where .= "and status = '" . $keyword . "' ";
                    break;
                case "dateline":
                    $where .= "and dateline = '" . $keyword . "' ";
                    break;
            }
        }
        //enddate 这里需要重构
        if (isset($_GET['startdate']) && isset($_GET['enddate']) && $_GET['startdate']) {
            $where .= " AND `regdate` >= '" . strtotime($_GET['startdate']) . "' AND `regdate` <= '" . strtotime($_GET['enddate']) . "' ";
        }
        return $where;
    }

    //表单数据
    private function post_frm() {
        $arr = array();
        $arr['upid'] = request::post('upid', '');
        $arr['uid'] = request::post('uid', '');
        $arr['idtype'] = request::post('idtype', '');
        $arr['id'] = request::post('id', '');
        $arr['message'] = request::post('message', '');
        $arr['ip'] = request::post('ip', '');
        $arr['status'] = request::post('status', '');
        $arr['dateline'] = time();
        return $arr;
    }

}
